package com.tjsinfo.tjsoa.oldOADocument.fawen.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.TXingzhengfawenFujian;
import com.tjsinfo.tjsoa.oldOADocument.fawen.bo.ITChaYueWenJianBO;
import com.tjsinfo.tjsoa.oldOADocument.fawen.vo.FaWenInfo;
/**
 * 原oa发文下载附件
 * @author wangjingjing
 *
 */
public class downloadFujianAction extends Action{

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		String url = "error";
		InputStream fis =  null;
		OutputStream toClient =  null;
		// TODO Auto-generated method stub
		try {
			String alias=request.getParameter("alias");
			String filename=URLDecoder.decode(request.getParameter("filename"),"utf-8");
			
			String FilePath = ToolKit.getProperties("com/tjsinfo/tjsoa/oldOADocument/fawen/properties/systemResource.properties", this).getProperty("oldoa.fujian.path");

			String desfile = FilePath + filename;
			desfile = ToolKit.delHTMLTag(desfile);
			ToolKit.copyFile(FilePath + alias,desfile); 
			File file = new File(desfile); 			
//			String filename = file.getName();
			 fis = new BufferedInputStream(new FileInputStream(desfile));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();		
			response.reset();
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(),"iso8859-1"));
			//arg3.addHeader("Content-Disposition ",   "attachment;filename="+ new String(xingzhengfawenFujian.getFujianName().getBytes(),"GBK"));
			response.addHeader("Content-Length", "" + file.length());
	         toClient = new BufferedOutputStream(response.getOutputStream());
	         response.setContentType("application/octet-stream");
	        toClient.write(buffer);
	        toClient.flush();
	        toClient.close();
			url = "ok";
		} catch (IOException ex) {
			request.setAttribute("returnSting", "服务器忙，请稍后下载！");
			ex.printStackTrace();
			return mapping.findForward("error");
		} catch(NullPointerException ex){
			return null;
		} catch (Exception ex){
			request.setAttribute("returnSting", "服务器忙，请稍后下载！");
			return mapping.findForward("error");
		}finally{
			fis.close();
			 toClient.close();
		}
		if ("ok".equals(url)) {
			return null;	
		}else {
			request.setAttribute("returnSting", "服务器忙，请稍后下载！");
			return  mapping.findForward(url);
		}
	}
	
}

